package com.yangj.servlet;

import javax.servlet.http.*;
import com.yangj.service.*;
import com.yangj.util.*;
import net.sf.json.*;
import javax.servlet.*;
import java.io.*;

public class GetUserInfoServlet extends HttpServlet
{
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Get请求");
        //获得code
        String code = req.getParameter("code");
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
        url = url.replace("APPID", WeChatVerifyService.getAppid()).replace("SECRET", WeChatVerifyService.getAppsecret()).replace("CODE", code);
        final String result = MyUtils.get(url);
        System.out.println(result);
        
        
        String at = JSONObject.fromObject((Object)result).getString("access_token");
        String openid = JSONObject.fromObject((Object)result).getString("openid");
        
        //拉取用户信息
        String url2 = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
        url2 = url2.replace("ACCESS_TOKEN", at).replace("OPENID", openid);
        final String result2 = MyUtils.get(url2);
        System.out.println(result2);
    }
    
    protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    }
}
